using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Web;

/// <summary>
/// Summary description for FileManager
/// </summary>
public class FileManager
{
    public static File GetFile(int FileID)
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("GetFile", connection))
            {
                // ROLES?
                bool booFilter = !(HttpContext.Current.User.IsInRole("Friends") || HttpContext.Current.User.IsInRole("Administrators"));

                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@FIL_ID", FileID));
                command.Parameters.Add(new SqlParameter("@IsPublic", booFilter));
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        File temp = new File(
                            (int)reader["FIL_ID"],
                            reader["FIL_NAME"].ToString(),
                            reader["FIL_LOCATION"].ToString(),
                            reader["FIL_DATE"].ToString(),
                            reader["FIL_NOTES"].ToString(),
                            reader["FIL_EXTENTION"].ToString(),
                            reader["FIL_VERSION"].ToString(),
                            Convert.ToBoolean(reader["FIL_ISPUBLIC"]));
                        return temp;
                    }// end if - reader read
                    else
                        return null;
                }//end reader using

            }// end command using
        }// END OF THE CONNECTION USING
    }// end of the GetFile method

    public static List<File> GetFiles()
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            List<File> list = new List<File>();

            using (SqlCommand command = new SqlCommand("GetFiles", connection))
            {
                // ROLES? 
                bool booFilter = !(HttpContext.Current.User.IsInRole("Friends") || HttpContext.Current.User.IsInRole("Administrators"));

                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@IsPublic", booFilter));
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        File temp = new File(
                            (int)reader["FIL_ID"],
                            reader["FIL_NAME"].ToString(),
                            reader["FIL_LOCATION"].ToString(),
                            reader["FIL_DATE"].ToString(),
                            reader["FIL_NOTES"].ToString(),
                            reader["FIL_EXTENTION"].ToString(),
                            reader["FIL_VERSION"].ToString(),
                            Convert.ToBoolean(reader["FIL_ISPUBLIC"]));
                        list.Add(temp);
                    }// end of the while to build the return list
                }//end reader using               
            }// end command using

            return list;
        }// end connection using
    }// end of the GetFiles method

    public static void AddFile(string Name, string Location, string Notes, string Extention, string Version, bool IsPublic)
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("AddFile", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@FIL_NAME", Name));
                command.Parameters.Add(new SqlParameter("@FIL_LOCATION", Location));
                command.Parameters.Add(new SqlParameter("@FIL_NOTES", Notes));
                command.Parameters.Add(new SqlParameter("@FIL_EXTENTION", Extention));
                command.Parameters.Add(new SqlParameter("@FIL_VERSION", Version));
                command.Parameters.Add(new SqlParameter("@FIL_ISPUBLIC", IsPublic));
                connection.Open();
                command.ExecuteNonQuery();
            }// end command using
        }// end connection using
    }// end of the AddFile method

    public static void RemoveFile(int FileID, string Location, string Name)
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("RemoveFile", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@FIL_ID", FileID));
                connection.Open();
                command.ExecuteNonQuery();
                try
                {
                    // now delete the file on the computer
                    System.IO.File.Delete(Location+"\\"+Name);
                }
                catch 
                {  
                }
            }// end command using
        }// end connection using
    }// end of the RemoveFile method

    public static void EditFile(int FileID, string Name, string Location, string Date, string Notes, string Extention, string Version, bool IsPublic)
    {
        using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Personal"].ConnectionString))
        {
            using (SqlCommand command = new SqlCommand("EditFile", connection))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add(new SqlParameter("@FIL_ID", FileID));
                //command.Parameters.Add(new SqlParameter("@FIL_NAMES", Name));
                //command.Parameters.Add(new SqlParameter("@FIL_LOCATION", Location));
                //command.Parameters.Add(new SqlParameter("@FIL_DATE", Date));
                command.Parameters.Add(new SqlParameter("@FIL_NOTES", Notes));
                //command.Parameters.Add(new SqlParameter("@FIL_EXTENTION", Extention));
                command.Parameters.Add(new SqlParameter("@FIL_VERSION", Version));
                command.Parameters.Add(new SqlParameter("@FIL_ISPUBLIC", IsPublic));
                connection.Open();
                command.ExecuteNonQuery();
            }// end command using
        }// end connection using
    }// end of the EditFile method
}// end of the class
